/*
 * 11. Write a program that reads a number n from
 * the console and prints the first n rows of the
 * Pascal's triangle.
 */

package _3;

import java.util.Scanner;

public class _11 {
	public static void main (String[] args) {
		Scanner scn = new Scanner(System.in);
		int n = 0;

		System.out.print("n ?= ");
		if (scn.hasNextInt())
			n = scn.nextInt();
		else {
			System.err.println("error: mismatched input");
			System.exit(1);
		}

		int[][] matrix = new int[2][n];
		int i, max = Integer.MIN_VALUE;

		for (i = n - 1; i >= 0; i--) {
			for (int j = 0; j <= n - 1 - i; j++) {
				if (j == 0 || j == n - 1 - i)
					matrix[1][j] = 1;
				else {
					matrix[1][j] = matrix[0][j - 1] + matrix[0][j];
					if (max < matrix[1][j])
						max = matrix[1][j];
				}
			}

			for (int j = 0; j <= n - 1 - i; j++)
				matrix[0][j] = matrix[1][j];
		}

		int nDigits = 1;
		while ((max /= 10) != 0)
			nDigits++;

		for (i = n - 1; i >= 0; i--) {
			for (int j = i; j >= 0; j--)
				System.out.printf("%" + nDigits + "c", ' ');

			for (int j = 0; j <= n - 1 - i; j++) {
				if (j == 0 || j == n - 1 - i) {
					matrix[1][j] = 1;
					System.out.printf("%" + nDigits + "d%" + nDigits + "c",
							matrix[1][j], ' ');
				} else {
					matrix[1][j] = matrix[0][j - 1] + matrix[0][j];
					System.out.printf("%" + nDigits + "d%" + nDigits + "c",
							matrix[1][j], ' ');
				}
			}
			System.out.println();

			for (int j = 0; j <= n - 1 - i; j++)
				matrix[0][j] = matrix[1][j];
		}
	}
}
